//
//  MCGridMenuView.m
//  MClient
//
//  Created by zhouyi on 10-11-2.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MCGridMenuView.h"
#import "MServerJsonDAO.h"

@implementation MCGridMenuCell

#define SafeRelease(p) if (p) {\
[p release]; p = nil;\
}

- (id)initWithFrame:(CGRect)frame withTarget:(id)deleg withSelector:(SEL)select {
    if ((self = [super initWithFrame:frame])) {
		[self addTarget:deleg action:select forControlEvents:UIControlEventTouchUpInside];
		
		icon = [[UIImageView alloc] initWithFrame:CGRectMake(16, 5, 48, 48)];
		[self addSubview:icon];
		
		btnLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 48+5, 80, 32)];
		btnLabel.font = [UIFont fontWithName:@"Helvetica" size:14.000];
		btnLabel.textColor = [UIColor blackColor];
		btnLabel.backgroundColor = [UIColor clearColor];
		btnLabel.textAlignment = UITextAlignmentCenter;
		btnLabel.numberOfLines = 2;
		[self addSubview:btnLabel];
		
		badgeValue = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, 30, 18)];
		badgeValue.font = [UIFont fontWithName:@"Helvetica" size:12.000];
		badgeValue.textColor = [UIColor whiteColor];
		badgeValue.backgroundColor = [UIColor clearColor];
		badgeValue.textAlignment = UITextAlignmentCenter;
		[self addSubview:badgeValue];
		
		self.showsTouchWhenHighlighted = YES;
    }
    return self;
}

-(NSString*)iconName {
	return nil;
}

-(void)setIconName:(NSString*)aIconName {
	icon.image = [UIImage imageNamed:aIconName];//设置按钮图片
}

-(NSString*)menuText {
	return btnLabel.text;
}

-(void)setMenuText:(NSString*)aMenuText {
	btnLabel.text = aMenuText;
}

- (NSMutableDictionary*)menuData {
	return _menuData;
}

- (void)setMenuData:(NSMutableDictionary *)aMenuData {
	[_menuData release];
	_menuData = [aMenuData retain];
	
	if ([[self.menuData objectForKey:@"showc"] boolValue] == YES) {
		if (![self.menuData objectForKey:@"listCount"]) {
			SafeRelease(_jsonDao);
			_jsonDao = [[MServerJsonDAO alloc] init];
			NSMutableDictionary *url = [self.menuData objectForKey:@"url"];
			[_jsonDao getListSizeAsync:[url objectForKey:@"dto"] == nil?@"":[url objectForKey:@"dto"]
							layoutName:[url objectForKey:@"lytn"] == nil?@"":[url objectForKey:@"lytn"]
						   queryFilter:[url objectForKey:@"qflt"] == nil?[[NSMutableArray alloc] init]:[url objectForKey:@"qflt"]
							  delegate:self];
			MClientAppDelegate *appDelegate = (MClientAppDelegate*)[[UIApplication sharedApplication] delegate];
			[appDelegate stopActivityIndicatorView];
		}else {
			badgeValue.text = [self.menuData objectForKey:@"listCount"];
			[self bringSubviewToFront:badgeValue];
		}
	}
}

- (void) getDataFromServerSuccess: (NSMutableDictionary *)returnValue {
	UIImage *image;
	if ([(NSString *)returnValue intValue] < 10) {
		image = [UIImage imageNamed:@"badgeValue1.png"];
		badgeValue.frame = CGRectMake(48, 0, 30, 18);
	} else if ([(NSString *)returnValue intValue] >= 10 && [(NSString *)returnValue intValue] < 100) {
		image = [UIImage imageNamed:@"badgeValue2.png"];
		badgeValue.frame = CGRectMake(51, 0, 30, 18);
	} else if ([(NSString *)returnValue intValue] >= 100 && [(NSString *)returnValue intValue] < 1000){
		image = [UIImage imageNamed:@"badgeValue3.png"];
		badgeValue.frame = CGRectMake(55, 0, 30, 18);
	} else {
		image = [UIImage imageNamed:@"badgeValue3.png"];
		badgeValue.frame = CGRectMake(54, 0, 30, 18);
	}
	UIImageView *imageView = (UIImageView *)[self viewWithTag:-99];
	if (!imageView) {
		imageView = [[UIImageView alloc] initWithFrame:CGRectMake(54, 0, image.size.width, image.size.height)];
		imageView.image = image;
		imageView.tag = -99;
		[self addSubview:imageView];
		[self bringSubviewToFront:imageView];
		[imageView release];
	} else {
		imageView.frame = CGRectMake(54, 0, image.size.width, image.size.height);
		imageView.image = image;
		[self bringSubviewToFront:imageView];
	}
	imageView.hidden = NO;
	badgeValue.hidden = NO;
	badgeValue.text = (NSString *)returnValue;
	if ([(NSString *)returnValue intValue] <= 0) {
		imageView.hidden = YES;
		badgeValue.hidden = YES;
	}
	[self bringSubviewToFront:badgeValue];
	[self.menuData setObject:returnValue forKey:@"listCount"];
}

- (void)dealloc {
	[[NSNotificationCenter defaultCenter] postNotificationName:@"viewControllerDealloc" object:self];
	[_menuData release];
	[_jsonDao release];
	[icon release];
	[badgeValue release];
	[btnLabel release];
	[_iconName release];
    [super dealloc];
}
@end

@implementation MCGridMenuView
@synthesize menuDelegate = _menuDelegate;
@synthesize menuSelector = _menuSelector;
@synthesize selected = _selected;
@synthesize isApp = _isApp;

- (id)initWithFrame:(CGRect)frame
{
	if (self = [super initWithFrame:frame]) {
		self.backgroundColor = [UIColor clearColor];
	}
	
	return self;
}

- (void)dealloc {
	[_menuData release];
	[super dealloc];
}

- (NSArray*) menuData {
	return _menuData;
}

- (void)setMenuData:(NSArray *)aMenuData {
	[_menuData release];_menuData=nil;
	_menuData = [aMenuData retain];
	for (int i = 0; i<[_menuData count]; i++) {
		NSMutableDictionary *menu = [_menuData objectAtIndex:i];
		NSString *imageFile = [menu objectForKey:@"icon"];
		
		if (imageFile == nil || [imageFile length] == 0) {
			imageFile = @"0.png";
		}
		
		MCGridMenuCell *gridMenuView = (MCGridMenuCell*)[self viewWithTag:i+kTagBase];
		if (nil == gridMenuView) {
			gridMenuView = [[MCGridMenuCell alloc] initWithFrame:CGRectZero 
													  withTarget:self.menuDelegate 
													withSelector:self.menuSelector];
			gridMenuView.tag = i+kTagBase;
			gridMenuView.iconName = imageFile;
			gridMenuView.menuText = [menu objectForKey:@"label"];
			gridMenuView.menuData = menu;
			
			[self addSubview:gridMenuView];
			[gridMenuView release];
		} else {
			gridMenuView.iconName = imageFile;
			gridMenuView.menuText = [menu objectForKey:@"label"];
			gridMenuView.menuData = menu;
		}
	}
	if ([MCUserDefaults instance].contactSync && _isApp) {
		MCGridMenuCell *gridMenuView = (MCGridMenuCell*)[self viewWithTag:[_menuData count]+kTagBase];
		if (nil == gridMenuView) {
			gridMenuView = [[MCGridMenuCell alloc] initWithFrame:CGRectZero 
													  withTarget:self.menuDelegate 
													withSelector:self.menuSelector];
			gridMenuView.tag = [_menuData count]+kTagBase;
			gridMenuView.iconName = @"30.png";
			gridMenuView.menuText = @"通讯录同步";
			gridMenuView.menuData = nil;
			
			[self addSubview:gridMenuView];
			[gridMenuView release];
		} else {
			gridMenuView.iconName = @"30.png";
			gridMenuView.menuText = @"通讯录同步";
			gridMenuView.menuData = nil;
		}
	}
}

- (void)layoutSubviews
{
	[super layoutSubviews];
	
	int nOneLineNum = CGRectGetWidth(self.bounds) / 105;
	if (nOneLineNum <= 0) {
		return;
	}
	int menuCount = [self.menuData count];
	if ([MCUserDefaults instance].contactSync && _isApp) {
		menuCount++;
	}
	int nAddition = (CGRectGetWidth(self.bounds) - 20 - nOneLineNum*105) / nOneLineNum;
	for (int i = 0; i<menuCount; i++) {
		[self viewWithTag:i+kTagBase].frame = CGRectMake(20+(i%nOneLineNum)*105+nAddition, 5+(i/nOneLineNum)*105, 80, 90);
	}
	self.contentSize = CGSizeMake(CGRectGetWidth(self.bounds), 5+(menuCount/nOneLineNum+1)*105);
}
@end